package com.qf.chat.service.impl;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.chat.commons.utils.UserUtils;
import com.qf.chat.dao.FriendRelationDao;
import com.qf.chat.entity.FriendRelation;
import com.qf.chat.entity.vo.FriendVo;
import com.qf.chat.service.FriendRelationService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 好友关系(FriendRelation)表服务实现类
 *
 * @author makejava
 * @since 2024-01-12 10:46:26
 */
@Service("friendRelationService")
public class FriendRelationServiceImpl extends ServiceImpl<FriendRelationDao, FriendRelation> implements FriendRelationService {

    /**
     * 判断两个id是否为好友关系
     * @param uid
     * @param fid
     * @return
     */
    @Override
    public boolean isFriendRelation(Integer uid, Integer fid) {
        return this.query().eq("uid", uid).eq("fid", fid).count() > 0;
    }

    /**
     * 添加好友关系
     * @param uid
     * @param fid
     * @param beizhu1
     * @param beizhu2
     * @return
     */
    @Override
    public int addFriendRelation(Integer uid, Integer fid, String beizhu1, String beizhu2) {

        FriendRelation relation1 = new FriendRelation()
                .setUid(uid)
                .setFid(fid)
                .setBeizhu(beizhu1);

        FriendRelation relation2 = new FriendRelation()
                .setUid(fid)
                .setFid(uid)
                .setBeizhu(beizhu2);

        return getBaseMapper().insertBatch(ListUtil.of(relation1, relation2));
    }

    /**
     * 查询我的好友列表
     * @return
     */
    @Override
    public List<FriendVo> queryMyFriends() {
        //获取当前用户id
        Integer uid = UserUtils.getUid();
        //查询当前用户的好友列表
        return getBaseMapper().queryMyFriends(uid);
    }
}

